import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from '../views/Home/index.vue'
import Login from '../views/Login/index.vue'

import Course from '../views/Home/Course/index.vue'
import Game from '../views/Home/Game/index.vue'
import News from '../views/Home/News/index.vue'

import Item from '../views/Home/Course/item.vue'
Vue.use(VueRouter)

const router = new VueRouter({
    routes:[
        {
            path:'/home',
            component:Home,
            children:[
                {
                    path:'course',
                    component:Course,
                    name:'course',
                    children:[
                        {
                            // path:'item/:id/:name',
                            path:'item/:id',
                            // path:'item',
                            // props:{a:1},
                            // props:true,
                            props:route=>({a:1,id:route.params.id,name:route.query.name}),
                            component:Item,
                            name:'item'
                        }
                    ]
                },
                {
                    path:'game',
                    component:Game,
                    name:'game'
                },
                {
                    path:'news',
                    component:News,
                    name:'news'
                },
                {
                    path:'',
                    redirect:'/home/course'
                }

            ]
        },
        {
            path:'/login',
            component:Login
        },
        {
            path:'/',
            redirect:'home'
        }
    ]
})

const originPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location, onResolved, onRejected) {
  console.log('push', onResolved, onRejected)
//   debugger
  // 如果传入了回调, 用的旧语法, 不会有错误的, 调用原函数进行处理
  if (onResolved || onRejected) {
    originPush.call(this, location, onResolved, onRejected)
  } else { // 没有传回调, 用的新语法, 可能会抛出失败的promise错误
    return originPush.call(this, location).catch(() => {
      console.log('error')
    })
    // originPush.call(this, () => {})
  }
}

export default router